import { useAppDispatch, useAppSelector } from "@/store/hooks";
import { submitWhiteAudit } from "@/store/whiteAudit/whiteAuditAPI";
import {
  changeModal,
  getWhiteAuditAsync,
} from "@/store/whiteAudit/whiteAuditSlice";
import { Form, Input, Modal, Select } from "antd";
import { useState, type ReactElement } from "react";

export default function Create(): ReactElement<typeof Modal, "div"> {
  const [loading, setLoading] = useState<boolean>(false);
  const { modal, params } = useAppSelector((state) => state.whiteAudit);
  const { config: { data: { data: { wd_review_whitelist = [] } = {} } = {} } = {} } =
    useAppSelector((state) => state.app);
  const dispatch = useAppDispatch();
  const handleClose = () => {
    dispatch(changeModal({ open: false }));
    form.resetFields();
  };
  const [form] = Form.useForm();
  const handleOk = async () => {
    try {
      const values = await form.validateFields();
      setLoading(true);
      submitWhiteAudit({
        android_id: values.android_id,
        app_params: "zxcvbn1",
        params_type: 1,
        model_name: values.model_name
      })
        .then(() => {
          handleClose();
          dispatch(getWhiteAuditAsync({ ...params }));
        })
        .finally(() => setLoading(false));
    } catch (err) {}
  };
  return (
    <Modal
      open={modal.open}
      title="创建"
      onCancel={handleClose}
      onOk={handleOk}
      confirmLoading={loading}
    >
      <Form form={form}>
        <Form.Item
          name="android_id"
          label="安卓ID"
          rules={[{ required: true, message: "请输入" }]}
        >
          <Input />
        </Form.Item>
        <Form.Item
          name="model_name"
          label="审核模块"
          rules={[{ required: true, message: "请输入" }]}
        >
          <Select options={wd_review_whitelist} />
        </Form.Item>
      </Form>
    </Modal>
  );
}
